package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.PeachApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.PeachBlossom;
import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.model.utils.ErrorResult;
import com.tanhua.model.vo.PeachBlossomVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lxb哦吼吼
 * @version 1.0
 * @description: TODO
 * @date 2021/11/17 15:02
 */

@Service
public class PeachBlossomService {

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private PeachApi peachApi;

    @DubboReference
    private UserInfoApi UserInfoApi;

    public void pushVoice(MultipartFile soundFile) throws IOException {
        Long userId = UserHolder.getUserId();
        //
        String pushKey = Constants.PEACH_PUSH_TIMES + userId;
        String times = (String) redisTemplate.opsForValue().get(pushKey);
        if ("0".equals(times)) {
            throw new BusinessException(ErrorResult.peachPushError());
        }
        //
        String url = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
        UserInfo userInfo = UserInfoApi.findById(userId);
        //
        PeachBlossom peachBlossom = new PeachBlossom();
        peachBlossom.setUserId(userId);
        peachBlossom.setSoundUrl(url);
        peachBlossom.setAge(userInfo.getAge());
        peachBlossom.setAvatar(userInfo.getAvatar());
        peachBlossom.setGender(userInfo.getGender());
        peachBlossom.setNickname(userInfo.getNickname());
        peachApi.saveVoice(peachBlossom);
        //
        redisTemplate.opsForValue().decrement(pushKey, 1);
    }

    public PeachBlossomVo pullVoice() {
        Long userId = UserHolder.getUserId();
        String pullKey = Constants.PEACH_PULL_TIMES + userId;
        String times = (String) redisTemplate.opsForValue().get(pullKey);
        if ("0".equals(times)) {
            throw new BusinessException(ErrorResult.peachPullError());
        }
        //
        PeachBlossom peachBlossom = peachApi.getVoiceRandom();
        PeachBlossomVo peachBlossomVo = new PeachBlossomVo();
        BeanUtil.copyProperties(peachBlossom, peachBlossomVo);
        //
        Long decrement = redisTemplate.opsForValue().decrement(pullKey, 1);
        peachBlossomVo.setRemainingTimes(Math.toIntExact(decrement));
        peachBlossomVo.setId(peachBlossom.getUserId());
        return peachBlossomVo;
    }
}
